package com.xca.photogallery.presenter;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.xca.photogallery.common.WeakRrfHandler;
import com.xca.photogallery.contract.DayImageContact;
import com.xca.photogallery.entity.DayImage;
import com.xca.photogallery.service.DayImageService;
import com.xca.photogallery.utils.LogUtils;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
//import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class DayImagePresenter implements DayImageContact.DayImagePresenter {
    public static final int HTTP_SUCCESS = 1 ;
    public static final int HTTP_FAIL = 0 ;
    private DayImageContact.DayImageView mMainView ;
    private Context mContext ;
    private OkHttpClient mClient ;
    public DayImagePresenter(DayImageContact.DayImageView mMainView , Context mContext){
        this.mMainView = mMainView ;
        this.mContext = mContext ;
        mClient = new OkHttpClient();
    }
    @Override
    public void getData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Request request = new Request.Builder()
                        .url("https://api.oioweb.cn/api/bing")
                        .build() ;
                Call call = mClient.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(@NonNull Call call, @NonNull IOException e) {

                    }

                    @Override
                    public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                        String body = response.body().string() ;
                        String url = call.request().url().toString() ;
                        Message message = Message.obtain() ;
                        message.obj = new String[]{body , url} ;
                        message.what = HTTP_SUCCESS ;
                        mHandler.sendMessage(message) ;
                    }
                });
            }
        }).start();

//        Retrofit retrofit = new Retrofit.Builder()
//                .baseUrl("https://api.oioweb.cn/api/")
//                .addConverterFactory(GsonConverterFactory.create())
//                .build();
//        DayImageService dayImageService = retrofit.create(DayImageService.class);
//        dayImageService.getData().enqueue(new Callback<ResponseBody>() {
//            @Override
//            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
//                try {
//                    String body = response.body().string();
//                    String url = call.request().url().toString();
//                    LogUtils.d("asdf", "url : " + url);
//                    LogUtils.d("asdf", "onResponse : " + body);
//                    Message message = Message.obtain();
//                    message.obj = new String[]{body, url};
//                    message.what = HTTP_SUCCESS;
//                    mHandler.sendMessage(message);
//                } catch (IOException e) {
//                    throw new RuntimeException(e);
//                }
//            }
//
//            @Override
//            public void onFailure(Call<ResponseBody> call, Throwable t) {
//                LogUtils.d("dayImage Failed" , "ERROR IN GET DATA");
//            }
//        });
    }

    WeakRrfHandler<DayImagePresenter> mHandler = new WeakRrfHandler<>(Looper.getMainLooper(), this, new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            int result = msg.what;
            switch (result) {
                case HTTP_SUCCESS:
                    String[] data = (String[]) msg.obj;
                    if (data == null || data.length < 2) {
                        return false;
                    }
                    String jsonStr = data[0];
                    Gson gson = new Gson();
                    try {
                        DayImage dayImage = gson.fromJson(jsonStr, DayImage.class);
                        if (mMainView != null) {
                            mMainView.showData(dayImage);
                        }
                    } catch (JsonSyntaxException e) {
                        LogUtils.d("asdf", "result not json");
                    }
                    break;

                case HTTP_FAIL:
                    break;
            }
            return false;
        }
    });
}
